package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.CompoundButton;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.checkbox.MaterialCheckBox;

import cn.lrwjz.androidguide.R;

public class CheckboxActivity extends AppCompatActivity {

    private MaterialCheckBox parentCheckbox;
    private MaterialCheckBox[] childCheckboxes;
    private boolean isUpdatingChildren = false;
    private boolean isUpdatingParent = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_checkbox);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Checkbox 示例");
        }

        // 初始化复选框
        initCheckboxes();
        setupCheckboxListeners();
    }

    private void initCheckboxes() {
        parentCheckbox = findViewById(R.id.checkbox_parent);
        childCheckboxes = new MaterialCheckBox[]{
            findViewById(R.id.checkbox_child_1),
            findViewById(R.id.checkbox_child_2),
            findViewById(R.id.checkbox_child_3),
            findViewById(R.id.checkbox_child_4)
        };
    }

    private void setupCheckboxListeners() {
        // 设置父复选框监听器
        parentCheckbox.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (!isUpdatingParent) {
                isUpdatingChildren = true;
                for (MaterialCheckBox childCheckbox : childCheckboxes) {
                    childCheckbox.setChecked(isChecked);
                }
                isUpdatingChildren = false;
            }
        });

        // 设置子复选框监听器
        CompoundButton.OnCheckedChangeListener childListener = (buttonView, isChecked) -> {
            if (!isUpdatingChildren) {
                isUpdatingParent = true;
                updateParentCheckbox();
                isUpdatingParent = false;
            }
        };

        for (MaterialCheckBox childCheckbox : childCheckboxes) {
            childCheckbox.setOnCheckedChangeListener(childListener);
        }
    }

    private void updateParentCheckbox() {
        int checkedCount = 0;
        for (MaterialCheckBox childCheckbox : childCheckboxes) {
            if (childCheckbox.isChecked()) {
                checkedCount++;
            }
        }

        if (checkedCount == 0) {
            parentCheckbox.setChecked(false);
            parentCheckbox.setCheckedState(MaterialCheckBox.STATE_UNCHECKED);
        } else if (checkedCount == childCheckboxes.length) {
            parentCheckbox.setChecked(true);
            parentCheckbox.setCheckedState(MaterialCheckBox.STATE_CHECKED);
        } else {
            parentCheckbox.setCheckedState(MaterialCheckBox.STATE_INDETERMINATE);
        }
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 